//
//  ShouYiView.m
//  SP2P
//
//  Created by Eternal on 2017/7/4.
//  Copyright © 2017年 EIMS. All rights reserved.
//

#import "ShouYiView.h"
#import "IQKeyboardManager.h"
@interface ShouYiView ()
{

   
   NSInteger tag;
}
//转入金额
@property (weak, nonatomic) IBOutlet UITextField *moneyTF;
//定期收益
@property (weak, nonatomic) IBOutlet UIButton *dqBtn;

@property (weak, nonatomic) IBOutlet UILabel *resultLable;
@property (weak, nonatomic) IBOutlet UIButton *backGroundBtn;


@end
@implementation ShouYiView

- (instancetype)initWithFrame:(CGRect)frame andViewType:(NSInteger)viewType
{
    self = [super initWithFrame:frame];
    if (self) {
        self = [[[NSBundle mainBundle]loadNibNamed:@"ShouYiView" owner:self options:nil]lastObject];
        self.backgroundColor = [[UIColor blackColor] colorWithAlphaComponent:0.4];
        [self.moneyTF addTarget:self action:@selector(limit:) forControlEvents:UIControlEventEditingChanged];
        self.dqBtn.userInteractionEnabled = NO;
        [self addGestureRecognizer:[[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(cancelButtonClick)]];
        self.backGroundBtn.layer.masksToBounds = YES;
        self.backGroundBtn.layer.cornerRadius = 15;
        self.dqBtn.layer.masksToBounds = YES;
        self.dqBtn.layer.cornerRadius = 6;
       
        tag = viewType;
        [IQKeyboardManager sharedManager].enable = NO;
    }
    return self;
}

- (void)cancelButtonClick{

    
    [UIView animateWithDuration:0.7 animations:^{
        [self.moneyTF resignFirstResponder];
        self.hidden = YES;
    }];
}

//计算到期收益
- (IBAction)calculateDqBtn:(UIButton *)sender {
    switch (tag) {
        case 1:
            
            self.resultLable.text = [NSString stringWithFormat:@"投资到期后可获得%.2f元收益",self.moneyTF.text.integerValue*0.09*3/12];
            break;
        case 2:
            self.resultLable.text = [NSString stringWithFormat:@"投资到期后可获得%.2f元收益",self.moneyTF.text.integerValue*0.095*6/12];
            break;
        case 3:
            self.resultLable.text = [NSString stringWithFormat:@"投资到期后可获得%.2f元收益",self.moneyTF.text.integerValue*0.1*12/12];
            break;
        default:
            break;
    }
    
}
//限制文本框的输入内容和文本的长度
- (void)limit:(UITextField *)textField{
    self.resultLable.text = @"";
    //限制文本的输入长度不得大于10个字符长度
    if (textField.text.length >= 7){
        
        //截取文本字符长度为10的内容
        textField.text = [textField.text substringToIndex:7];
    }
    if (self.moneyTF.text.doubleValue > 0) {
        [self.dqBtn setBackgroundColor:[UIColor orangeColor]];
        self.dqBtn.userInteractionEnabled = YES;
    }else{
        [self.dqBtn setBackgroundColor:[UIColor colorWithRed:250/255.0 green:168/255.0 blue:119/255.0 alpha:1.0]];
        self.dqBtn.userInteractionEnabled = NO;
    }
    
}
@end
